package org.musicsweet.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.musicsweet.constant.StringConstant;
import org.musicsweet.entity.SysDic;
import org.musicsweet.entity.SysUser;
import org.musicsweet.mapper.SysDicMapper;
import org.musicsweet.result.ResultData;
import org.musicsweet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 系统字典Service
 */
@Slf4j
@Service
public class SysDicService {

    @Autowired
    private SysDicMapper sysDicMapper;

    @Autowired
    private SysUserService sysUserService;


    /**
     * 根据id查询系统字典
     *
     * @param sysDic
     * @return
     */
    public ResultData<SysDic> getSysDicById(SysDic sysDic) {
        log.info("根据id查询系统字典:" + JSON.toJSONString(sysDic));
        if (StringUtils.isEmpty(sysDic.getId())) {
            return ResultData.createFail("id不能为空");
        }
        sysDic.setDel(StringConstant.DEL_NO);
        SysDic data = sysDicMapper.getSysDicById(sysDic);
        return ResultData.successData("查询成功",data);
    }

    /**
     * 多条件查询系统字典
     *
     * @param sysDic
     * @return
     */
    public ResultData<List<SysDic>> getSysDicList(SysDic sysDic) {
        log.info("多条件查询系统字典:" + JSON.toJSONString(sysDic));
        sysDic.setDel(StringConstant.DEL_NO);
        List<SysDic> data = sysDicMapper.getSysDicList(sysDic);
        return ResultData.successData("查询成功",data);
    }


    /**
     * 添加系统字典
     *
     * @param sysDic
     * @param token
     * @return
     */
    @Transactional
    public ResultData addSysDic(SysDic sysDic, String token) {
        sysDic.setId(StringUtil.getUUID());
        sysDic.setCreateTime(new Date());
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        sysDic.setCreateUserId(currentUser.getId());
        sysDic.setDel(StringConstant.DEL_NO);
        log.info("添加系统字典:" + JSON.toJSONString(sysDic));
        int i = sysDicMapper.addSysDic(sysDic);
        if (i > 0) {
            return ResultData.createSuccess(null);
        } else {
            return ResultData.createFail("添加系统字典失败");
        }
    }


    /**
     * 修改系统字典
     *
     * @param sysDic
     * @return
     */
    @Transactional
    public ResultData updateSysDic(SysDic sysDic, String token) {
        if (StringUtils.isEmpty(sysDic.getId())) {
            return ResultData.createFail("id不能为空");
        }
        SysUser currentUser = sysUserService.getSysUserByToken(token).getData();
        sysDic.setUpdateUserId(currentUser.getId());
        sysDic.setUpdateTime(new Date());
        log.info("修改系统字典:" + JSON.toJSONString(sysDic));
        int i = sysDicMapper.updateSysDic(sysDic);
        if (i > 0) {
            return ResultData.createSuccess(null);
        } else {
            return ResultData.createFail("修改系统字典失败");
        }
    }


    /**
     * 删除系统字典
     *
     * @param sysDic
     * @return
     */
    @Transactional
    public ResultData delSysDic(SysDic sysDic) {
        if (StringUtils.isEmpty(sysDic.getId())) {
            return ResultData.createFail("id不能为空");
        }
        sysDic.setDel(StringConstant.DEL_OK);
        log.info("删除系统字典:" + JSON.toJSONString(sysDic));
        int i = sysDicMapper.deleteSysDic(sysDic);
        if (i > 0) {
            return ResultData.createSuccess(null);
        } else {
            return ResultData.createFail("删除系统字典失败");
        }
    }
}
